package com.ttn.student.service.impl;

import cn.hutool.core.util.NumberUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ttn.common.core.entity.Result;
import com.ttn.common.core.entity.business.CourseUnitGrammarTest;
import com.ttn.common.core.entity.studentrecord.GrammarTestStudyRecordDetails;
import com.ttn.common.core.entity.studentrecord.LearningProgressRecord;
import com.ttn.common.core.entity.studentrecord.TestStudyRecord;
import com.ttn.common.core.entity.studentrecord.query.TestStudyRecordQuery;
import com.ttn.common.core.entity.studentrecord.vo.TestStudyRecordDetailsVO;
import com.ttn.student.mapper.GrammarTestStudyRecordDetailsMapper;
import com.ttn.student.service.GrammarTestStudyRecordDetailsService;
import com.ttn.student.service.LearningProgressRecordService;
import com.ttn.student.service.TestStudyRecordService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.List;
import java.util.Objects;

@Service
public class GrammarTestStudyRecordDetailsServiceImpl extends ServiceImpl<GrammarTestStudyRecordDetailsMapper, GrammarTestStudyRecordDetails> implements GrammarTestStudyRecordDetailsService {
    @Resource
    private GrammarTestStudyRecordDetailsMapper grammarTestStudyRecordDetailsMapper;
    @Resource
    private TestStudyRecordService testStudyRecordService;
    @Resource
    private LearningProgressRecordService learningProgressRecordService;

    @Transactional
    @Override
    public Result saveGrammarTestStudyRecordDetails(GrammarTestStudyRecordDetails grammarTestStudyRecordDetails) {
        TestStudyRecordDetailsVO testStudyRecordDetailsVO = new TestStudyRecordDetailsVO();
        if (grammarTestStudyRecordDetails.getAnswer().equals(grammarTestStudyRecordDetails.getUserAnswer())) {
            grammarTestStudyRecordDetails.setAnswerFlag(1);
        } else {
            grammarTestStudyRecordDetails.setAnswerFlag(0);
        }
        this.save(grammarTestStudyRecordDetails);
        TestStudyRecord testStudyRecord = testStudyRecordService.getById(grammarTestStudyRecordDetails.getStudyRecordId());
        LambdaQueryWrapper<GrammarTestStudyRecordDetails> grammarTestStudyRecordDetailsLambdaQueryWrapper = new LambdaQueryWrapper<>();
        grammarTestStudyRecordDetailsLambdaQueryWrapper.eq(GrammarTestStudyRecordDetails::getStudyRecordId, grammarTestStudyRecordDetails.getStudyRecordId());
        Integer studyNum = Math.toIntExact(this.count(grammarTestStudyRecordDetailsLambdaQueryWrapper));
        testStudyRecord.setStudyNum(studyNum);
        testStudyRecordService.updateById(testStudyRecord);
        //计算分数和计算当前超越人数
        if (studyNum.equals(testStudyRecord.getCourseUnitNum())) {
            //获取答对题目数
            LambdaQueryWrapper<GrammarTestStudyRecordDetails> grammarTestStudyRecordDetailsLambdaQueryWrapper1 = new LambdaQueryWrapper<>();
            grammarTestStudyRecordDetailsLambdaQueryWrapper1.eq(GrammarTestStudyRecordDetails::getAnswerFlag, 1);
            grammarTestStudyRecordDetailsLambdaQueryWrapper1.eq(GrammarTestStudyRecordDetails::getStudyRecordId, grammarTestStudyRecordDetails.getStudyRecordId());

            Long correctAnswer = this.count(grammarTestStudyRecordDetailsLambdaQueryWrapper1);
            testStudyRecord.setCorrectAmount(Math.toIntExact(correctAnswer));
            correctAnswer = correctAnswer * 100;
            testStudyRecord.setScore(NumberUtil.div(correctAnswer, testStudyRecord.getCourseUnitNum()).intValue());
            LambdaQueryWrapper<TestStudyRecord> testStudyRecordLambdaQueryWrapper = new LambdaQueryWrapper<>();
            //获取所有用户数量
            testStudyRecordLambdaQueryWrapper.eq(TestStudyRecord::getCourseUnitId, grammarTestStudyRecordDetails.getCourseUnitId());
            testStudyRecordLambdaQueryWrapper.eq(TestStudyRecord::getFinishFlag, 1);
            testStudyRecordLambdaQueryWrapper.eq(TestStudyRecord::getUnitTaskType, "3");
            long countAll = testStudyRecordService.count(testStudyRecordLambdaQueryWrapper);
            if (countAll != 0) {
                //获取超过的用户
                //获取超过的用户
                testStudyRecordLambdaQueryWrapper.eq(TestStudyRecord::getFinishFlag, 1);
                testStudyRecordLambdaQueryWrapper.gt(TestStudyRecord::getScore, testStudyRecord.getScore());
                long beyondProportionNum = testStudyRecordService.count(testStudyRecordLambdaQueryWrapper);
                //更新得分和排名
                testStudyRecord.setBeyondProportion(BigDecimal.valueOf(NumberUtil.mul(NumberUtil.sub(1, NumberUtil.div(beyondProportionNum, countAll, 2)), 100)));

            } else {
                testStudyRecord.setBeyondProportion(BigDecimal.valueOf(100));
            }
            testStudyRecordDetailsVO.setTestStudyRecord(testStudyRecord);
            //获取词汇错题集
            grammarTestStudyRecordDetailsLambdaQueryWrapper.eq(GrammarTestStudyRecordDetails::getAnswerFlag, 0);
            testStudyRecordDetailsVO.setGrammarTestStudyRecordDetails(this.list(grammarTestStudyRecordDetailsLambdaQueryWrapper));
            testStudyRecord.setFinishFlag(1);
            testStudyRecordService.updateById(testStudyRecord);
            LambdaQueryWrapper<LearningProgressRecord> learningProgressRecordLambdaQueryWrapper =
                    new LambdaQueryWrapper<>();
            learningProgressRecordLambdaQueryWrapper.eq(LearningProgressRecord::getStudentId, testStudyRecord.getStudentId());
            learningProgressRecordLambdaQueryWrapper.eq(LearningProgressRecord::getCourseUnitId, testStudyRecord.getCourseUnitId());
            LearningProgressRecord learningProgressRecord =
                    learningProgressRecordService.getOne(learningProgressRecordLambdaQueryWrapper);
            learningProgressRecord.setCorrectAmount(testStudyRecordService.queryTotalCorrectAmountByUnitIdId(testStudyRecord.getCourseUnitId(), testStudyRecord.getStudentId()));
//            learningProgressRecord.setFinishFlag(1);
            learningProgressRecordService.updateById(learningProgressRecord);
            return Result.success(testStudyRecordDetailsVO);
        }
        return Result.success();
    }

    @Override
    public Result queryGrammarTestStudyRecordDetails(TestStudyRecordQuery testStudyRecordQuery) {
        //获取分数最高的一次测试记录
        TestStudyRecordDetailsVO testStudyRecordDetailsVO = new TestStudyRecordDetailsVO();
        LambdaQueryWrapper<TestStudyRecord> testStudyRecordLambdaQueryWrapper = new LambdaQueryWrapper<>();
        testStudyRecordLambdaQueryWrapper.eq(TestStudyRecord::getStudentId, testStudyRecordQuery.getStudentId());
        testStudyRecordLambdaQueryWrapper.eq(TestStudyRecord::getCourseUnitId, testStudyRecordQuery.getCourseUnitId());
        testStudyRecordLambdaQueryWrapper.eq(TestStudyRecord::getUnitTaskType, "3");
        testStudyRecordLambdaQueryWrapper.eq(TestStudyRecord::getFinishFlag, 1);
        testStudyRecordLambdaQueryWrapper.orderByDesc(TestStudyRecord::getScore);
        testStudyRecordLambdaQueryWrapper.last(" limit 1");
        TestStudyRecord testStudyRecord = testStudyRecordService.getOne(testStudyRecordLambdaQueryWrapper);
        if (Objects.nonNull(testStudyRecord)) {
            LambdaQueryWrapper<TestStudyRecord> testStudyRecordLambdaQueryWrapper1 = new LambdaQueryWrapper<>();
            //获取所有用户数量
            testStudyRecordLambdaQueryWrapper1.eq(TestStudyRecord::getUnitTaskType, "3");
            testStudyRecordLambdaQueryWrapper1.eq(TestStudyRecord::getCourseUnitId, testStudyRecordQuery.getCourseUnitId());
            long countAll = testStudyRecordService.count(testStudyRecordLambdaQueryWrapper1);
            if (countAll != 0) {
                //获取超过的用户
                testStudyRecordLambdaQueryWrapper1.eq(TestStudyRecord::getFinishFlag, 1);
                testStudyRecordLambdaQueryWrapper1.gt(TestStudyRecord::getScore, testStudyRecord.getScore());
                long beyondProportionNum = testStudyRecordService.count(testStudyRecordLambdaQueryWrapper1);
                //更新得分和排名
                testStudyRecord.setBeyondProportion(BigDecimal.valueOf(NumberUtil.mul(NumberUtil.sub(1, NumberUtil.div(beyondProportionNum, countAll, 2)), 100)));
            } else {
                testStudyRecord.setBeyondProportion(BigDecimal.valueOf(100));
            }
            testStudyRecordDetailsVO.setTestStudyRecord(testStudyRecord);
            LambdaQueryWrapper<GrammarTestStudyRecordDetails> grammarTestStudyRecordDetailsLambdaQueryWrapper = new LambdaQueryWrapper<>();
            grammarTestStudyRecordDetailsLambdaQueryWrapper.eq(GrammarTestStudyRecordDetails::getStudyRecordId,
                    testStudyRecord.getId());
            //获取词汇错题集
            grammarTestStudyRecordDetailsLambdaQueryWrapper.eq(GrammarTestStudyRecordDetails::getAnswerFlag, 0);
            testStudyRecordDetailsVO.setGrammarTestStudyRecordDetails(this.list(grammarTestStudyRecordDetailsLambdaQueryWrapper));
        }
        return Result.success(testStudyRecordDetailsVO);
    }

    @Override
    public List<CourseUnitGrammarTest> getUnfinishedCourseUnitGrammarTestList(TestStudyRecord testStudyRecord) {
        return grammarTestStudyRecordDetailsMapper.getUnfinishedCourseUnitGrammarTestList(testStudyRecord);
    }
}
